#include "toast.h"
#include <QDebug>

Toast::Toast(QWidget *parent) : QLabel(parent)
{
    setAlignment(Qt::AlignCenter);
    setStyleSheet("background-color: rgba(0, 0, 0, 70%); color: white; border-radius: 5px; padding: 8px;");
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_ShowWithoutActivating);

    QFont font = this->font();
    font.setPointSize(50);
    setFont(font);

    // 使用定时器控制显示时间
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &Toast::hide);
}

void Toast::showMessage(QWidget* parent, const QString& message, int duration) {
    Toast* toast = new Toast(parent);
    toast->setText(message);
    toast->adjustSize();

    // 计算显示位置（居中）
    if (parent) {
        QPoint center = parent->rect().center();
        qDebug() << "QPoint value:" << center.x() << center.y()
        << "move to" << center.x() - toast->width() / 2 << center.y() - toast->height() / 2;
        toast->move(center.x() - toast->width() / 2,
                    center.y() - toast->height() / 2);
    }

    toast->show();
    toast->m_timer->start(duration);

    // 自动删除
    QTimer::singleShot(duration, toast, &Toast::deleteLater);
}
